package com.jiliang.auth.controller;


import com.jiliang.auth.controller.req.AuthRoleReqForm;
import com.jiliang.auth.controller.res.AuthRoleResForm;
import com.jiliang.auth.dao.model.AuthRole;
import com.jiliang.auth.domain.AuthRoleBo;
import com.jiliang.auth.domain.AuthRoleMemberBo;
import com.jiliang.auth.service.AuthRoleMemberService;
import com.jiliang.auth.service.AuthRoleService;
import com.jiliang.common.bean.BeanCopierDsUtil;
import com.jiliang.common.bean.BeanCopyDsUtil;
import com.jiliang.common.constant.CommonDsConstant;
import com.jiliang.common.result.WebResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 角色管理controller
 *
 * @author 
 * @version 1.0
 * @Date  15:52
 */
@RestController
@RequestMapping(value = "/api/authRole")
public class AuthRoleController {
    private Logger log = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private AuthRoleService authRoleService;
    @Autowired
    private AuthRoleMemberService authRoleMemberService;

//    @Autowired
//    private SystemDictItemService systemDictItemService;



    @RequestMapping(value = "/selRoleListBySysId", method = RequestMethod.POST)
    public WebResult selRoleListBySysId(@RequestBody AuthRoleReqForm reqForm, BindingResult result) {
        AuthRoleBo authRoleBo = new AuthRoleBo();
        BeanCopyDsUtil.copyProperties(reqForm, authRoleBo);
        List<AuthRoleBo> authRoleBos = authRoleService.selRoleListBySysId(authRoleBo);
        List<AuthRoleResForm> roleResFormList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(authRoleBos)) {
            roleResFormList = BeanCopierDsUtil.copyPropertiesOfList(authRoleBos, AuthRoleResForm.class);
        }
        return WebResult.success(roleResFormList);
    }


    @RequestMapping(value = "/selRoleListByPage", method = RequestMethod.POST)
    public WebResult selRoleListByPage(@RequestBody AuthRoleReqForm roleReqForm, BindingResult result) {

        AuthRoleBo authRoleBo = new AuthRoleBo();
        BeanCopyDsUtil.copyProperties(roleReqForm, authRoleBo);
        Integer pageNum = roleReqForm.getPageNum();
        Integer pageSize = roleReqForm.getPageSize();
        if (pageNum == null || pageNum == 0) {
            pageNum = CommonDsConstant.PAGE_NUM;
        }
        if (pageSize == null || pageSize == 0) {
            pageSize = CommonDsConstant.PAGE_SIZE;
        }
        PageHelper.startPage(pageNum, pageSize);
        List<AuthRole> authRoleList = authRoleService.selListByParam(authRoleBo);
        PageInfo pageInfo = (PageInfo) new PageInfo<>(authRoleList);
        List<AuthRoleResForm> roleResFormList = modelToResList(authRoleList);
        if(roleResFormList ==null || roleResFormList.size()==0){
            PageInfo<AuthRoleResForm> resFormPageInfo = new PageInfo<AuthRoleResForm>();
            return WebResult.success(resFormPageInfo);
        }
        pageInfo.setList(roleResFormList);
        return WebResult.success(pageInfo);
    }

    /**
     * 根据角色编码查询授权角色信息
     *
     * @param roleReqForm 角色请求表单，包含角色编码等信息
     * @return WebResult 包含查询到的角色响应表单
     */
    @RequestMapping(value = "/selAuthRoleByCode", method = RequestMethod.POST)
    public WebResult selAuthRoleByCode(@RequestBody AuthRoleReqForm roleReqForm) {
        // 通过角色编码查询数据库中的角色信息
        AuthRole authRole = authRoleService.selAuthRoleByCode(roleReqForm.getRoleCode());

        // 将查询到的角色模型转换为角色响应表单
        AuthRoleResForm roleResForm = modelToRes(authRole);

        // 返回查询结果，使用WebResult包装成功消息和角色响应表单
        return WebResult.success(roleResForm);
    }

    /**
     * 根据角色代码更新角色信息
     * 该方法通过角色代码来更新角色信息，包括角色名称、描述等
     * 使用POST请求方式，接收一个JSON格式的角色信息表单
     *
     * @param roleReqForm 角色信息请求表单，包含要更新的角色信息
     * @return WebResult 返回一个WebResult对象，表示操作结果
     */
    @RequestMapping(value = "/updAuthRoleByCode", method = RequestMethod.POST)
    public WebResult updAuthRoleByCode(@RequestBody AuthRoleReqForm roleReqForm) {
        // 创建一个新的AuthRole对象，用于存储角色信息
        AuthRole authRole = new AuthRole();
        // 使用工具类将角色信息请求表单的数据复制到AuthRole对象中
        BeanCopyDsUtil.copyProperties(roleReqForm, authRole);
        // 调用服务层方法，根据角色代码更新角色信息
        authRoleService.updAuthRoleByCode(authRole);
        // 返回成功结果
        return WebResult.success();
    }


    @RequestMapping(value = "/delAuthRoleByCode", method = RequestMethod.POST)
    public WebResult delAuthRoleByCode(@RequestBody AuthRoleReqForm roleReqForm) {

        AuthRoleMemberBo roleMemberBo = new AuthRoleMemberBo();
        roleMemberBo.setSystemId(roleReqForm.getSystemId());
        roleMemberBo.setSubSystemId(roleReqForm.getSubSystemId());
        roleMemberBo.setRoleCodes(roleReqForm.getRoleCode());
        List<AuthRoleMemberBo> authRoleMemberBos = authRoleMemberService.selectByCode(roleMemberBo);
        if (CollectionUtils.isNotEmpty(authRoleMemberBos)) {
            return WebResult.fail("1", "删除失败", "请先删除该角色下的用户，再删除角色", null);
        }
        authRoleService.delAuthRoleByCode(roleReqForm.getRoleCode());
        return WebResult.success();
    }

    /**
     * 新增角色
     * @param roleReqForm
     * @return
     */
    @RequestMapping(value = "/addAuthRole", method = RequestMethod.POST)
    public WebResult addAuthRole( @RequestBody AuthRoleReqForm roleReqForm) {
        AuthRole authRole = new AuthRole();
        BeanCopyDsUtil.copyProperties(roleReqForm, authRole);
        return WebResult.success(authRoleService.addAuthRole(authRole));
    }

    private List<AuthRoleResForm> modelToResList(List<AuthRole> authRoleList) {
        if (CollectionUtils.isEmpty(authRoleList)) {
            return null;
        }
        List<AuthRoleResForm> result = new ArrayList<AuthRoleResForm>();
        AuthRoleResForm resForm = null;
        for (AuthRole authRole : authRoleList) {
            resForm = modelToRes(authRole);
            result.add(resForm);
        }
        return result;
    }

    private AuthRoleResForm modelToRes(AuthRole authRole) {
        if (authRole == null) {
            return null;
        }
        AuthRoleResForm resForm = BeanCopierDsUtil.copyProperties(authRole, AuthRoleResForm.class);
        resForm.setCount(authRoleService.countByRoleCode(authRole.getRoleCode()));
        resForm.setFuncList(authRoleService.selFuncListByRoleCode(authRole.getRoleCode()));
        if(authRole.getSystemId() != null){

//            SystemDictionaryItem item = new SystemDictionaryItem();
//            item.setSystemId(authRole.getSystemId());
//            item.setDictId("APP_URL");
//            item.setCode("APP_URL");
//            item = systemDictItemService.selItemByDictIdCodeSysId(item);
//            if(item != null){
//                resForm.setSystemName(item.getDescription());
//            }
        }
        return resForm;
    }

    /**
     * 根据用户id查询角色列表
     * @param boId
     * @return
     */
     @GetMapping("/selRoleListByBoId")
     public WebResult selRoleListByBoId(@RequestParam("boId") String boId) {
         //根据用户id查询包含角色列表
         List<String> roleCodes = authRoleMemberService.selRoleListByBoId(boId);
         if (CollectionUtils.isEmpty(roleCodes)){
             return WebResult.success(new ArrayList<>());
         }
         AuthRoleBo authRoleBo = new AuthRoleBo();
         authRoleBo.setRoleCodeList(roleCodes);
         List<AuthRole> authRoleList = authRoleService.selListByParam(authRoleBo);
         List<AuthRoleResForm> roleResFormList = modelToResList(authRoleList);
         return WebResult.success(roleResFormList);
     }
}
